﻿using AutoMapper;
using KayakMes.Common;
using KayakMes.Common.Result;
using KayakMes.RBAC.Application.Input;
using KayakMes.RBAC.Application.OUT;
using KayakMes.RBAC.Doamin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace KayakMes.RBAC.Application.Supper
{
    /// <summary>
    /// 供应商服务
    /// </summary>
    public class SupperServer : ISupperServer
    {
        private readonly BaseReposiotry<SupplierModel> _supplierReposiotry;

        private readonly IMapper mapper;
        public SupperServer(BaseReposiotry<SupplierModel> supplierReposiotry, IMapper mapper)
        {
            _supplierReposiotry = supplierReposiotry;
            this.mapper = mapper;
        }
        /// <summary>
        /// 添加供应商
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> AddSupper(AddsupperInput input)
        {
            var model=mapper.Map<SupplierModel>(input);
            model.Id=YitIdHelper.NextId();
            model.CreateTime=DateTime.Now;
            var obj=_supplierReposiotry.Add(model);
            if(obj>0)
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Success,
                    Message = "添加供应商成功",
                    Data = obj
                };
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Message = "添加供应商失败",
                    Data = 0
                };
            }

        }
        /// <summary>
        /// 获取供应商 下拉列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<List< SupperXiaOUT>> GetSupper()
        {
            var model=_supplierReposiotry.GetAll().ToList();
            var data= mapper.Map<List<SupperXiaOUT>>(model);
            return new ApiResult<List<SupperXiaOUT>>()
            {
                Code = ApiEnums.Success,
                Message = "获取供应商成功",
                Data = data
            };
        }
        /// <summary>
        /// 获取供应商详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<SupperOut> GetSupperById(long id)
        {
            var model=_supplierReposiotry.GetModel(id);
            var data= mapper.Map<SupperOut>(model);
            return new ApiResult<SupperOut>()
            {
                Code = ApiEnums.Success,
                Message = "获取供应商详情成功",
                Data = data
            };
        }
        /// <summary>
        /// 获取供应商列表分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="SupperCoide"></param>
        /// <param name="SupperName"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiPaging<SupperOut> GetSupperList(int pageIndex, int pageSize, string? SupperCoide, string? SupperName)
        {
            try
            {
                // 构建基础查询
                var query = _supplierReposiotry.GetAll().Where(x => x.IsDeleted == false);
                
                // 添加查询条件
                if (!string.IsNullOrEmpty(SupperCoide))
                {
                    query = query.Where(x => x.SupplierCode.Contains(SupperCoide));
                }
                if (!string.IsNullOrEmpty(SupperName))
                {
                    query = query.Where(x => x.SupplierName.Contains(SupperName));
                }
                
                // 获取总数
                var total = query.Count();
                
                // 分页查询数据
                var data = query.OrderBy(x => x.Id)
                               .Skip((pageIndex - 1) * pageSize)
                               .Take(pageSize)
                               .ToList();
                
                return new ApiPaging<SupperOut>()
                {
                    Code = ApiEnums.Success,
                    Message = "获取供应商列表成功",
                    Items = mapper.Map<List<SupperOut>>(data),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    Total = total
                };
            }
            catch (Exception ex)
            {
                return new ApiPaging<SupperOut>()
                {
                    Code = ApiEnums.Fail,
                    Message = $"获取供应商列表失败：{ex.Message}",
                    Items = new List<SupperOut>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    Total = 0
                };
            }
        }
        /// <summary>
        /// 修改供应商
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> UpdateSupper(UpdateSupperInput input)
        {
            var model=_supplierReposiotry.GetModel(input.Id);
            mapper.Map(input, model);
            var result=_supplierReposiotry.Update(model);
            if(result>0)
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Success,
                    Message = "修改供应商成功",
                    Data = result
                };
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Message = "修改供应商失败",
                    Data = result
                };
            }

        }
    }
}
